<script setup lang="ts">
import { onLaunch, onShow, onHide } from "@dcloudio/uni-app";
import { request, catchException } from "@/util/request";
import { initConfig } from "@/model/wxSDK/index";
import { updateApp } from "@/model/updateApp/updateApp";
import { gotoLogin } from "@/model/authToLogin/auth";
import { platform } from "@/util/platform";
import { navigate } from "@/util/navigate";
import { env } from "@/config/config";
import store from "@/store/store";

onLaunch(async () => {
  if (env === "dev") {
    // uni.setStorageSync(
    //   "userToken_zh_web",
    //   JSON.stringify({
    //     token: "699f08883803fdde456e8388d37c50eb",
    //     qr_code: "10ZEKS07I3",
    //     account: "01030104000001",
    //     nickname: "那谁",
    //   })
    // );
  } else {
    getLocalData();
  }
  uni.hideTabBar();
  uni.setStorageSync("sdkParams", "{}");
  // if (env === "prod") navigate({ url: "/", navMethod: "reLaunch" });
  updateApp();
  await getLocalData();
  await setGlobalState();
  getCityList();
  // gotoLogin();
});

onShow(() => {});
onHide(() => {});
function setGlobalState() {
  const { programState } = store;
  const systemInfo = uni.getSystemInfoSync();
  const {
    screenHeight,
    windowHeight,
    windowTop,
    windowBottom,
    statusBarHeight = 0,
    platform: system = "",
  } = systemInfo;
  const allWindowHeight = windowHeight + windowTop + windowBottom;
  programState.$patch({
    allWindowHeight,
    statusBarHeight,
    windowHeight,
    system,
    platform,
    screenHeight,
  });
  setTitleBarHeight();
  if (platform === "H5") getBrowserType();
}
function setTitleBarHeight() {
  const { programState } = store;
  const { system } = programState;
  let titleBarHeight: number;
  switch (system) {
    case "android": {
      titleBarHeight = 48;
      break;
    }
    case "ios": {
      titleBarHeight = 44;
      break;
    }
    default: {
      titleBarHeight = 44;
      break;
    }
  }
  switch (platform) {
    case "H5": {
      titleBarHeight = 50;
      break;
    }
  }
  programState.titleBarHeight = titleBarHeight;
}
async function getBrowserType() {
  const { programState } = store;
  const ua = navigator.userAgent.toLowerCase();
  const browser = {
    mobile: ua.includes("mobile"),
    ipad: ua.includes("ipad"),
    iphone: ua.includes("iphone"),
    android: ua.includes("android"),
    wx: ua.includes("micromessenger"),
    windows: ua.includes("windows"),
    mac: ua.includes("macintosh"),
  };
  programState.$patch({ browser });
  if (browser.wx) {
    {
      initConfig({
        jsApiList: [
          "scanQRCode",
          "getLocation",
          "showMenuItems",
          "updateTimelineShareData",
          "updateAppMessageShareData",
          "wx-open-launch-weapp"
        ],
        openTagList: ["wx-open-launch-weapp"],
      });
    }
  } else {
    if (env === "dev") return;
    navigate({
      url: "/pages/authBlank/index",
      navMethod: "reLaunch",
      query: { type: "error" },
    });
  }
}
async function getLocalData() {
  const { programState } = store;
  const { userState } = store;
  const account = uni.getStorageSync("account");
  let userToken = JSON.parse(uni.getStorageSync("userToken_zh_web") || "{}");
  userState.account = account;

  /* if (env === "dev" && !userToken.token) {
    userToken = { token: "ef7db10ed4e168e68635f419f1113958", qr_code: "" };
  } */
  userState.userToken = userToken;
}
async function getCityList() {
  const { globalState } = store;
  const cityList = JSON.parse(uni.getStorageSync("cityList") || "[]");
  if (cityList.length) {
    globalState.cityList = cityList;
    return;
  }
  try {
    const res = await request({ loading: false, url: "text/getCityAll" });
    globalState.cityList = res.data.content;
    uni.setStorageSync("cityList", JSON.stringify(res.data.content));
  } catch (error) {
    catchException(error);
  }
}
</script>

<style lang="scss">
@import "uview-plus/index.scss";
/* #ifdef H5 */
body {
  overflow-y: scroll !important;
  user-select: auto;
}
/* #endif */
page {
  height: 100%;
}
view {
  box-sizing: border-box;
}
view,
text {
  letter-spacing: 0.4rpx;
}
image {
  flex-shrink: 0;
}
button {
  display: flex !important;
  align-items: center;
  justify-content: center;
  &::after {
    border: none !important;
  }
}
</style>
